<script>
	import {
		goLogin,
		getWechatOpenId
	} from '@/api/global.js'
	import qs from 'qs'
	export default {
		data(){
			return {
				//本地测试数据
				// openId: 'abcdefghijklmnopqrstuvwxyz',
				// branchId: 27 || 43,
				// bussinessId: 60 || 78,
				// accountId:60 || 78,
				//正式环境数据
				accountId: 3,
				openId:'',
				branchId: 2,
				bussinessId: 3,
			}
		},
		onLaunch:  function(options) {
			console.log('App Launch',options)
			let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
			console.log(extConfig,123)
			// if(uni.getStorageSync('expirDate')){
			// 	if(JSON.parse(uni.getStorageSync('expirDate')).time < new Date().getTime()){
			// 		uni.removeStorageSync('expirDate');
			// 		uni.clearStorageSync();
			// 		uni.redirectTo({
			// 			url:'/pages/login/login?type=outLogin'
			// 		})
			// 	}
			// }else{
				if (options?.query?.scene) {
					let path = decodeURIComponent(options?.query?.scene);
					console.log('app.vue',path,'扫码路径')
					path = path.split('_');
					console.log(path[0],path[1])
					if(path[1] == 2){//邀请码
						uni.switchTab({
							url:'/pages/home/home'
						})
						 this.init(extConfig,'',path[0]);
					}else{//商品详情、职员码
						 this.init(extConfig,options);
					}
				}else{
					 this.init(extConfig,options);
				}
			// }
			
		},
		onShow: function() {
		console.log('onshow');
		},
		onHide: function() {
		},
		methods: {
			//首次进入获取唯一标识换取token
			async init(extConfig,options='',inviterId='') {
				console.log('app init 1')
				let onlyId = await this.getOnlyId(extConfig);
				console.log(onlyId)
				console.log('app init 2')
				if (onlyId) {
					//调用接口 
					let res = await goLogin({
						branchId: extConfig.branchId || this.branchId, //分支机构id[关联分支机构表]
						bussinessId: extConfig.bussinessId || this.bussinessId, //商户id[关联商户表]
						accountId: extConfig.accountId || this.accountId, //账套id[关联账套表]
						openId: onlyId, //微信openId
						uuid: onlyId,
						inviterId
					})
					if (res.code == 2000) {
						console.log(res);
						console.log('app init 3', res?.data?.info?.MallMember?.status )
						console.log(res?.data?.info?.visitorStatus,'没有开启游客模式' );
						// 先判断是否是会员 是会员 直接进 不是会员 在判断是否开启游客模式
						if (res?.data?.info?.MallMember?.status == 1) {
							//根据状态判断是否是开启游客模式 开启则进入首页 没开启则去登录 绑定手机号
							if (res?.data?.info?.visitorStatus == 2) { //1.开启 2.没有开启
								//存储临时token
								uni.setStorageSync('visitorAccessToken', JSON.stringify(res.data.info.accessToken))
								this.$store.commit('updateToken', res.data.info.accessToken)
								await this.$isResolve() //标记等这走完 转同步在main.js中挂载 home首页监听
								if (options?.query?.scene) {
									let path = decodeURIComponent(options?.query?.scene);
									console.log(path,'扫码路径')
									path = path.split('_');
									console.log(path[0],path[1],'商品id')
									if(path[1] == 1){
										uni.navigateTo({
											url:`/page_public/commodityDetail?commodityId=${path[0]}&scan=1`
										})
									}else if(path[1] == 3){
										uni.setStorageSync('staffInviteId',path[0]);
										uni.switchTab({
											url:'/pages/home/home?noOpen=1'
										})
									}
									return;
								}
								uni.reLaunch({
									// url: '/pages/login/login?type=noVisitorStatus'
									url:'/pages/home/home?noOpen=1'
								})
								return;
							}else{
								// 存储账户信息
								console.log(1111111111111);
								uni.setStorageSync('visitorLoginInfo', JSON.stringify(res.data.info))
								//存储临时token
								uni.setStorageSync('visitorAccessToken', JSON.stringify(res.data.info.accessToken));
								uni.setStorageSync('expirDate',JSON.stringify({time:new Date().getTime()+this.expireDate}));
								this.$store.commit('updateToken', res.data.info.accessToken)
								console.log(909,uni.getStorageSync('visitorLoginInfo'))
								await this.$isResolve() //标记等这走完 转同步在main.js中挂载 home首页监听
								if (options?.query?.scene) {
									let path = decodeURIComponent(options?.query?.scene);
									console.log(path,'扫码路径')
									path = path.split('_');
									console.log(path[0],path[1],'商品id')
									if(path[1] == 1){
										uni.navigateTo({
											url:`/page_public/commodityDetail?commodityId=${path[0]}`
										})
									}else if(path[1] == 3){
										uni.setStorageSync('staffInviteId',path[0]);
										uni.switchTab({
											url:'/pages/home/home'
										})
									}
									return;
								}
							}
							
						}else{
							
							uni.setStorageSync('visitorLoginInfo', JSON.stringify(res.data.info))
							//存储临时token
							uni.setStorageSync('visitorAccessToken', JSON.stringify(res.data.info.accessToken))
							uni.setStorageSync('expirDate',JSON.stringify({time:new Date().getTime()+this.expireDate}));
							this.$store.commit('updateToken', res.data.info.accessToken)
							//获取商城系统参数设置
							this.$store.dispatch('configSetting/getSetting');
							await this.$isResolve() //标记等这走完 转同步在main.js中挂载 home首页监听
							console.log(options)
							if (options?.query?.scene) {
								let path = decodeURIComponent(options?.query?.scene);
								console.log(path,'扫码路径')
								path = path.split('_');
								console.log(path[0],path[1],'商品id')
								if(path[1] == 1){
									uni.navigateTo({
										url:`/page_public/commodityDetail?commodityId=${path[0]}`
									})
								}else if(path[1] == 3){
									uni.setStorageSync('staffInviteId',path[0]);
									uni.switchTab({
										url:'/pages/home/home'
									})
								}
								return;
							}
							uni.reLaunch({
								// url: '/pages/login/login?type=noVisitorStatus'
								url:'/pages/home/home'
							})
						}
					}else{
						this.Utils.toast(`${res.msg}`);
					}
				}
			},
			//获取唯一标识
			getOnlyId(extConfig) {
				return new Promise(async (resolve) => {
					// #ifdef MP-WEIXIN
					uni.login({
						success: (res) => {
							console.log(res)
							//调用后台接口 获取oppenid
							getWechatOpenId({
								bussinessId:extConfig.bussinessId || this.bussinessId,
								accountId:extConfig.accountId || this.accountId,
								code: res.code,
								mark:1 //1.小程序 2.公众号网页
							}).then(resC => {
								if (resC.code == 2000) {
									resolve(resC.data.info.openId);
									console.log('openid',resC.data.info.openId)
									uni.setStorageSync('wxOpenId',resC.data.info.openId);
									uni.setStorageSync('userAllId',JSON.stringify({
										bussinessId:extConfig.bussinessId || this.bussinessId,
										branchId:extConfig.branchId || this.branchId,
										accountId:extConfig.accountId || this.accountId
									}));
									this.$store.commit('updateShanghu',{
										bussinessId:extConfig.bussinessId || this.bussinessId,
										branchId:extConfig.branchId || this.branchId,
										accountId:extConfig.accountId || this.accountId
									})
								}else{
									this.Utils.toast(`${resC.msg}`);
								}
							})
						}
					})
					// #endif
				})
			}
		}
	}
</script>

<style lang="scss">
	.custom_checkbox /deep/.u-checkbox__icon-wrap--square{
		width:36rpx!important;
		height:36rpx!important;
		border-radius:50%!important;
		.u-icon__icon{
			font-size:24rpx!important;
		}
	}
	.active /deep/.u-checkbox__icon-wrap--square{
		background-color: #ec4141!important;
		border-color: #ec4141!important;
		.u-icon__icon{
			color:#fff!important;
		}
	}
	.ql-align-right{
		text-align: right!important;
	}
	.ql-align-center{
		text-align:center!important;
	}
	.u-content{
		text-align: justify!important;
		padding:0 6rpx!important;
	}
	/*每个页面公共css */
	@import "uview-ui/index.scss";

	/*页面背景色*/
	.page_body {
		height: 100vh;
		background-color: $bg_color;
	}

	.page {
		/* #ifdef H5 */
		height: calc(100vh - 50px);
		box-sizing: border-box;
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
	}

	.pd_lr10 {
		padding: 0 20rpx;
	}

	.pd_left10 {
		padding-left: 20rpx;
	}

	.ptb10 {
		padding: 20rpx 0;
	}

	.pd10 {
		padding: 20rpx;
	}

	.pd5 {
		padding: 10rpx;
	}

	.mt10 {
		margin-top: 20rpx;
	}

	.mtb10 {
		margin: 20rpx 0;
	}

	.mb10 {
		margin-bottom: 20rpx;
	}

	.mr10 {
		margin-right: 20rpx;
	}

	.ml5 {
		margin-left: 10rpx;
	}

	.mb10 {
		margin-bottom: 20rpx;
	}

	/*二行显示*/
	.texttowShow {
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow-wrap: anywhere;
	}

	/*一行显示*/
	.textOneShow {
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		overflow-wrap: anywhere;
	}

	/* 复选框形状 */
	/deep/ .uni-checkbox-input {
		border-radius: 50% !important;
	}

	/* ifdef MP-WEIXIN */
	/*checkbox 选项框大小  */
	/deep/ checkbox .wx-checkbox-input {
		border-radius: 50% !important;
	}

	/* endif */

	/* 复选框大小 */
	/deep/.uni-checkbox-input:hover {
		border-color: $text_color_ec4141 !important;
	}

	/* 状态栏背景色 */
	.ztl {
		width: 100%;
		background-color: #FFF;
	}

	/* 提示框文字居中 */
	/deep/.u-modal__content__text {
		text-align: center !important;
	}
</style>